#include <sstream>
using namespace std;

#include <misc/error.h>
#include "mysql-utils.h"

mysqlpp::StoreQueryResult 
MySqlUtils::q_single_row(mysqlpp::Connection* conn, const char* q)
{
  mysqlpp::Query query = conn->query(q);
  mysqlpp::StoreQueryResult res = query.store();
  if (!res) {
    ostringstream msg;
    msg << "MySQLUtils::q_signle_row: query failed, q was " << q;
    throw Error(__FILE__, __LINE__, msg.str());
  }

  if (res.num_rows() > 1) {
    ostringstream msg;
    msg << "MySQLUtils::q_single_row: more than one row returns, q was "
	<< q;
    throw Error(__FILE__, __LINE__, msg.str());
  }

  return res;
}

mysqlpp::StoreQueryResult 
MySqlUtils::q(mysqlpp::Connection* conn, const char* q)
{
  mysqlpp::Query query = conn->query(q);
  mysqlpp::StoreQueryResult res = query.store();
  if (!res) {
    ostringstream msg;
    msg << "MySqlUtils::q_single_col: query failed, q was " << q;
    throw Error(__FILE__, __LINE__, msg.str());
  }
  return res;
}
